package com.css.dzj.dzjjcss.modules.press.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditAsyncService;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.press.entity.Press;
import com.css.dzj.dzjjcss.modules.press.service.PressService;
import com.css.dzj.dzjjcss.modules.press.vo.PressVo;
import com.css.dzj.dzjjcss.modules.resultregister.entity.ResultRegisterEntity;
import com.css.dzj.dzjjcss.modules.statistics.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 催办信息Controller
 *
 * @author Leon
 */
@RestController
@RequestMapping("/press")
public class PressController {
    @Autowired
    private PressService pressService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 新增
     *
     * @param press
     * @return
     */
    @GetMapping("/add")
    public R add(@RequestBody Press press) {
        String s;
        try {
            pressService.add(press);
            s = JSON.toJSONString(press);
            sysAuditService.saveSuccessAudit("新增催办成功",Constant.CB, Constant.PT,Constant.ADD,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(press);
            sysAuditService.saveFailAudit("新增催办失败",Constant.CB,Constant.PT,Constant.ADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param press
     * @return
     */
    @GetMapping("/update")
    public R update(@RequestBody Press press) {
        String s;
        try {
            pressService.update(press);
            s = JSON.toJSONString(press);
            sysAuditService.saveSuccessAudit("修改催办成功",Constant.CB, Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(press);
            sysAuditService.saveFailAudit("修改催办失败",Constant.CB,Constant.PT,Constant.UPDATE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取催办信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getOne")
    public R queryById(@RequestParam String id) {
        String s;
        try {
            Press press = pressService.queryById(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("获取催办成功",Constant.CB, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", press);
        } catch (Exception e) {
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("获取催办失败",Constant.CB,Constant.PT,Constant.SELETE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取催办信息和主表信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getAll")
    public R queryAllById(@RequestParam String id) {
        String s;
        try {
            Press press = pressService.queryById(id);
            String linkId = press.getLinkId();
            ResultRegisterEntity registerEntity = statisticsService.queryById(linkId);
            PressVo pressVo = new PressVo();
            pressVo.setPress(press);
            pressVo.setRegisterEntity(registerEntity);
            s = JSON.toJSONString(pressVo);
            sysAuditService.saveSuccessAudit("获取催办和主表详情成功",Constant.CB, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", pressVo);
        } catch (Exception e) {
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("获取催办和主表详情失败",Constant.CB,Constant.PT,Constant.SELETE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @GetMapping("/logicDel")
    public R logicDel(@RequestParam String id) {
        String s;
        try {
            pressService.logicDel(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("逻辑删除催办成功",Constant.CB, Constant.PT,Constant.DEL,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("逻辑删除催办失败",Constant.CB, Constant.PT,Constant.DEL,s,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 物理删除
     *
     * @param id
     * @return
     */
    @GetMapping("/realDel")
    public R realDel(@RequestParam String id) {
        String s;
        try {
            pressService.realDel(id);
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("物理删除催办成功",Constant.CB, Constant.PT,Constant.DEL,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("物理删除催办成功",Constant.CB, Constant.PT,Constant.DEL,s,request);
            return R.error(e.getMessage());
        }
    }
}
